/*
 * @Author: szx
 * @Date: 2021-12-25 14:04:55
 * @LastEditTime: 2021-12-25 14:04:55
 * @Description:
 * @FilePath: \leetcode\700-799\701\701.js
 */
/**
 * @param {TreeNode} root
 * @param {number} val
 * @return {TreeNode}
 */
var insertIntoBST = function (root, val) {
    let node = root;
    let prev = node;
    while (node) {
        prev = node;
        node = val < node.val ? node.left : node.right;
    }
    node = new TreeNode(val);
    if (prev) {
        if (val < prev.val) {
            prev.left = node;
        } else {
            prev.right = node;
        }
    } else {
        return node;
    }
    return root;
};
